import { SubscribeMessage, WebSocketGateway, WebSocketServer, OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect } from '@nestjs/websockets';
import { Client, Server } from 'socket.io';

@WebSocketGateway()
export class EventsGateway implements
  OnGatewayInit,
  OnGatewayConnection,
  OnGatewayDisconnect { // 实现生命周期的接口


  @WebSocketServer()
  server: Server // server实例,在这里即namespace为'/'的server

  //#region 生命周期事件

  // 初始化
  afterInit(server: Server) {
    console.log("afterInit===========", server)
  }

  // 连接
  handleConnection(client: Client) {
    console.log("handleConnection===========", client);
  }

  // 断开连接
  handleDisconnect(client: Client) {
    console.log("handleDisconnect===========", client);
  }

  //#endregion

  // 注册事件
  @SubscribeMessage('event')
  handle(client: Client, roomId: string) {
    console.log("handle===========", client,roomId);
    // this.server.emit('event', data.username) // 在当前命名空间下广播事件
    return 'Hello World!' // 客户端接收的消息
  }

}
